home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <stdlib.h>
-
- #define HUNK_CODE 1001
-
- typedef int int32;
-
- inline int32
- ntohl (char *n)
- {
- return *(int32 *)n;
- }
-
- inline int
- read_long (FILE *fd, int32 *i)
- {
- char buf[4];
-
- fread (buf, 4, 1, fd);
- if (feof (fd))
- return 0;
- if (ferror (fd))
- {
- perror ("fread");
- return -1;
- }
- *i = ntohl (buf);
- return 1;
- }
-
- int
- main (int argc, char **argv)
- {
- int32 type;
- FILE *in;
- int diag;
-
- if (argc != 3)
- {
- fprintf (stderr, "usage: extract infile outfile\n");
- exit (8);
- }
- in = fopen (argv[1], "r");
- if (!in)
- {
- perror (argv[1]);
- exit (9);
- }
- while ((diag = read_long (in, &type)) == 1)
- {
- int32 size;
-
- fprintf (stderr, "Hunk type %d\n", type);
-
- switch (read_long (in, &size))
- {
- case 0:
- fprintf (stderr, "Unexpected end of file\n");
- fclose (in);
- exit (2);
- break;
- case -1:
- fclose (in);
- exit (11);
- }
- size *= 4;
- if (type == HUNK_CODE)
- {
- void *code = malloc (size);
- FILE *out;
- if (!code)
- {
- fprintf (stderr, "Out of memory\n");
- fclose (in);
- exit (7);
- }
- fread (code, size, 1, in);
- if (feof (in))
- {
- fprintf (stderr, "Unexpected end of file\n");
- free (code);
- fclose (in);
- exit (4);
- }
- if (ferror (in))
- {
- perror ("fread");
- free (code);
- fclose (in);
- exit (5);
- }
- fclose (in);
- out = fopen (argv[2], "w");
- if (!out)
- {
- perror (argv[2]);
- exit (10);
- }
- fwrite (code, size, 1, out);
- free (code);
- if (ferror (out))
- {
- perror ("fwrite");
- fclose (out);
- exit (6);
- }
- fclose (out);
- exit (0);
- }
- else
- {
- if (fseek (in, size, 1))
- {
- perror ("fseek");
- fclose (in);
- exit (3);
- }
- }
- }
- fclose (in);
- return diag == -1 ? 12 : 0;
- }
-